<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="登录帐号"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-dict code="SEX" v-model="dataForm.sex"></el-dict>
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input v-model="dataForm.realName" placeholder="真实姓名"></el-input>
      </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="dataForm.email" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="dataForm.mobile" />
        </el-form-item>

      <el-form-item label="所属机构" prop="orgNo">
        <el-dialog
          width="30%"
          append-to-body
          title="选择区域"
          :visible.sync="visiblePopover">
          <el-tree
            :data="orgNoOptions"
            :props="orgListTreeProps"
            node-key="orgNo"
            ref="orgListTree"
            @current-change="orgListTreeCurrentChangeHandle"
            default-expand-all
            :expand-on-click-node="false">
          </el-tree>
        </el-dialog>
        <el-button @click="visiblePopover = true">{{dataForm.orgName||'点击选择所属机构'}}</el-button>
      </el-form-item>
      <el-form-item label="角色" prop="roleIdList">
        <el-select v-model="dataForm.roleIdList" multiple clearable filterable placeholder="请选择" class="width185">
          <el-option
            v-for="role in roleList"
            :key="role.roleId"
            :label="role.roleName"
            :value="role.roleId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="管理员类型" size="mini" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="1">普通管理员</el-radio>
          <el-radio :label="2">商户管理员</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-show="dataForm.type==2">
        <el-form-item label="商户全称" prop="merchantFullName">
          <el-input v-model="dataForm.merchantFullName" :disabled="disabled" placeholder="商户全称"></el-input>
        </el-form-item>
        <el-form-item label="商户简称" prop="merchantAbbreviation">
          <el-input v-model="dataForm.merchantAbbreviation" :disabled="disabled" placeholder="商户简称"></el-input>
        </el-form-item>
        <el-form-item label="入驻时间" prop="timeOfEntry">
          <el-date-picker type="datetime" v-model="dataForm.timeOfEntry" :disabled="disabled"
                          value-format="yyyy-MM-dd HH:mm:ss" placeholder="入驻时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="合同签订日" prop="contractSigningDate">
          <el-date-picker type="datetime" v-model="dataForm.contractSigningDate" :disabled="disabled"
                          value-format="yyyy-MM-dd HH:mm:ss" placeholder="合同签订日"></el-date-picker>
        </el-form-item>
        <el-form-item label="合同起止日" prop="contractDate">
          <el-date-picker type="datetime"  v-model="dataForm.contractDate" :disabled="disabled"
                          value-format="yyyy-MM-dd HH:mm:ss" placeholder="合同起止日"></el-date-picker>
        </el-form-item>
        <el-form-item label="客服电话" prop="serviceMoblie">
          <el-input v-model="dataForm.serviceMoblie" :disabled="disabled" placeholder="客服电话"></el-input>
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactName">
          <el-input v-model="dataForm.contactName" :disabled="disabled" placeholder="联系人姓名"></el-input>
        </el-form-item>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name:"dataForm",
  data () {
    var checkEmail = (rule, value, cb) => {
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
      if (regEmail.test(value)) {
        return cb();
      }
      cb(new Error("请输入正确的邮箱"));
    };
    var checkMobile = (rule, value, cb) => {
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
      if (regMobile.test(value)) {
        return cb();
      }
      cb(new Error("请输入正确的手机号"));
    };
    var validateserviceMoblie = (rule, value, callback) => {
      let reg = /(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\(\d{3}\))|(\d{3}\-))?(1[3578]\d{9})$)|(^(400)-(\d{3})-(\d{4})(.)(\d{1,4})$)|(^(400)-(\d{3})-(\d{4}$))/;
      if (value == '') {
        callback(new Error("客服电话不能为空"));
      } else if (!reg.test(value)) {
        callback(new Error("客服电话填写有误"));
      }
      callback();
    };
    return {
      orgNoOptions: this.treeDataTranslate(JSON.parse(sessionStorage.getItem('orgList') || '[]'), 'orgNo', 'parentNo'),
      orgListTreeProps: {
        label: 'orgName',
        children: 'children'
      },
      visible: false,
      visiblePopover: false,
      disabled: false,
      roleList: [],
      dataForm: {
        orgName: '',
        id: '',
        userName: '',
        sex: '',
        realName: '',
        salt: '',
        email: '',
        mobile: '',
        orgNo: '',
        roleIdList: [],
        status: 1,
        type: '',
        merchantFullName: '',
        merchantAbbreviation: '',
        timeOfEntry: '',
        contractSigningDate: '',
        contractDate: '',
        serviceMoblie: '',
        contactName: '',
        sysUserId: ''
      },
      dataRule: {
        userName: [{
          required: true,
          message: '用户名不能为空',
          trigger: 'blur'
        }],
        sex: [{
          required: true,
          message: '性别不能为空',
          trigger: 'blur'
        }],
        realName: [{
          required: true,
          message: '真实姓名不能为空',
          trigger: 'blur'
        }],
        email: [{
            required: true,
            message: "请输入邮箱地址",
            trigger: "blur"
          },
          {
            validator: checkEmail,
            message: "请输入正确的邮箱地址",
            trigger: "blur"
          }],
        mobile: [{ required: true, message: "手机号码不能为空" },
          {
            validator: checkMobile,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }],
      },
      testRule:{
        merchantFullName: [{
          required: true,
          message: '商户全称不能为空',
          trigger: 'blur'
        }],
        merchantAbbreviation: [{
          required: true,
          message: '商户简称不能为空',
          trigger: 'blur'
        }],
        serviceMoblie: [{
          required: true,
          validator: validateserviceMoblie,
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id
      this.$http({
        url: '/sys/role/select',
        method: 'get'
      }).then(({ data }) => {
        this.roleList = data && data.code === 0 ? data.list : []
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (this.dataForm.id) {
          this.$http({
            url: `/sys/user/info/${this.dataForm.id}`,
            method: 'get'
          }).then(({ data }) => {
              console.log(data)
            if (data && data.code === 0) {
              this.dataForm.userName = data.user.userName
              this.dataForm.sex = data.user.sex.toString()
              this.dataForm.realName = data.user.realName
              this.dataForm.salt = data.user.salt
              this.dataForm.email = data.user.email
              this.dataForm.mobile = data.user.mobile
              this.dataForm.orgNo = data.user.orgNo
              this.dataForm.orgName = this.transOrg(data.user.orgNo)
              this.dataForm.roleIdList = data.user.roleIdList
              this.dataForm.status = data.user.status
              this.dataForm.type = data.user.type
              this.dataForm.merchantFullName = data.shopsManager.merchantFullName
              this.dataForm.merchantAbbreviation = data.shopsManager.merchantAbbreviation
              this.dataForm.timeOfEntry = data.shopsManager.timeOfEntry
              this.dataForm.contractSigningDate = data.shopsManager.contractSigningDate
              this.dataForm.contractDate = data.shopsManager.contractDate
              this.dataForm.serviceMoblie = data.shopsManager.serviceMoblie
              this.dataForm.contactName = data.shopsManager.contactName
            }
          })
        } else {
          this.dataForm.orgName = ''
        }
      })
    },
    // 机构树选中
    orgListTreeCurrentChangeHandle (data) {
      this.dataForm.orgNo = data.orgNo
      this.dataForm.orgName = data.orgName
      this.visiblePopover = false
    },
    // 表单提交
    dataFormSubmit () {
      // if( this.dataForm.type !== 1 ) {
      //   this.dataRule = {...(this.dataRule),...(this.testRule)}
      // }else {
      //   this.dataRule = this.dataRule
      // }
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dataForm.userId = this.dataForm.id
          this.$http({
            url: `/sys/user/${!this.dataForm.id ? 'save' : 'update'}`,
            method: 'post',
            data: this.dataForm
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.visible = false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.$emit('refreshDataList')
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped>
.mod-org {

.org-list__input,
.icon-list__input {

> .el-input__inner {
  cursor: pointer;
}

}
&
__icon-popover {
  max-width: 350px;
}

&
__icon-list {
  max-height: 380px;
  padding: 0;
  margin: -8px 0 0 -8px;

> .el-button {
  padding: 8px;
  margin: 8px 0 0 8px;

> span {
  display: inline-block;
  vertical-align: middle;
  width: 18px;
  height: 18px;
  font-size: 18px;
}

}
}
.icon-list__tips {
  font-size: 18px;
  text-align: center;
  color: #e6a23c;
  cursor: pointer;
}

}
</style>
